package com.hnevc.androiduiclassone9;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private  static final String TAG = "MainActivity";

    Button mBtnsh,mBtnbj,mBtngz;
    TextView mtvname;
    private  List<Weather> weathers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();

        //gson
        String json1 = "{\"name\":\"zhangsan\",\"age\":27,\"married\":true}";
        Gson gson = new Gson();
        Person person = gson.fromJson(json1,Person.class);
        Log.e(TAG,"dd"+person.getName());

        String json2 = "[16,2,26]";
        Type listType = new TypeToken<Integer>(){}.getType();
        List<Integer> ages = gson.fromJson(json2, listType);
        Log.e(TAG,"oncreate:"+ages.get(2));

        String json3 = "[" +
                "  {\"temp\":\"20℃/30℃\",\"weather\":\"晴转多云\",\"name\":\"上海\",\"pm\":\"80\",\"wind\":\"1级\"}," +
                "  {\"temp\":\"15℃/24℃\",\"weather\":\"晴\",\"name\":\"北京\",\"pm\":\"98\",\"wind\":\"3级\"}," +
                "  {\"temp\":\"26℃/32℃\",\"weather\":\"多云\",\"name\":\"广州\",\"pm\":\"30\",\"wind\":\"2级\"}" +
                "]";
        listType = new TypeToken<List<Weather>>(){}.getType();
        //将文件转换成READ
        InputStream in = getResources().openRawResource(R.raw.weather2);
        InputStreamReader reader = new InputStreamReader(in);
         weathers = gson.fromJson(reader,listType);
        Log.e(TAG,"oncreate:"+weathers.get(2).getWeather());



    }

    private void initEvent() {
        mBtnbj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather weather = weathers.get(1);

                mtvname.setText(weather.getName());
            }
        });
        mBtngz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather weather = weathers.get(2);
                mtvname.setText(weather.getName());
            }
        });
        mBtnsh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Weather weather = weathers.get(3);
                mtvname.setText(weather.getName());
            }
        });
    }

    private void initView() {
        mBtnbj = findViewById(R.id.btbeijing);
        mBtnsh= findViewById(R.id.btshanghai);
        mBtngz = findViewById(R.id.btguangzou);

        mtvname = findViewById(R.id.TVname);

    }
}
